let token_name = 'token'
let self = 'self'

// 设置cookie
export function setCookie(c_name, cvalue, exdays) {
  let date = new Date()
  date.setTime(date.getTime() + (exdays * 24 * 60 * 60 * 1000))
  let expires = 'expires=' + date.toUTCString()
  document.cookie = c_name + '=' + cvalue + '; ' + expires
}

// 获取cookie
export function getCookie(c_name) {
  var name = c_name + '='
  var ca = document.cookie.split(';')
  for (var i = 0; i < ca.length; i++) {
    var c = ca[i]
    while (c.charAt(0) == ' ') c = c.substring(1)
    if (c.indexOf(name) != -1) {
      return c.substring(name.length, c.length)
    }
  }
  return ''
}

// 清除cookie
export function clearCookie(c_name) {
  setCookie(c_name, '', -1)
}

// 设置token信息
export const setToken = (phone) => {
  console.log('设置token信息到sessionStorage')
  sessionStorage.setItem(token_name, phone)
}
// 获取登录信息，这里的登录信息就是手机号码
export const getAccessToken = () => {
  return sessionStorage.getItem(token_name)
}

// 保存个人中心信息
export const setSelfInfo = (data) => {
  sessionStorage.setItem(self, JSON.stringify(data))
}
// 获取个人中心信息
export const getSelfInfo = () => {
  return JSON.parse(sessionStorage.getItem(self))
}
// 登出，清理session
export const logout = () => {
  sessionStorage.removeItem(token_name)
  sessionStorage.removeItem(self)
}

const loginNames = ['orderFood', 'UploadHealth', 'AddUser', 'UserInfoDetail', 'PersonalCenter', 'UserInfo', 'IllnessReport', 'IllnessReportManager', 'LunchManager', 'PersonManager', 'NotifyManager', 'NotifyDetail', 'HotelInfo'
]
// 判断是否需要登录
export const needLogin = (name) => {
  let res = loginNames.filter(loginName => loginName == name)
  if (res.length > 0) {
    return true
  } else {
    return false
  }
}
